package api

import (
	"fmt"
	"github.com/fatih/color"
	"github.com/gin-gonic/gin"
	"web-collect-server/entity"
	"web-collect-server/entity/common/response"
	"web-collect-server/global"
)

func InitHeart(r *gin.Engine) {
	api := r.Group("/logper")
	api.POST("heartbeat", heart)

}

func heart(c *gin.Context) {
	var device entity.BanDevice
	ip := c.ClientIP() // 获取请求 的ip
	color.Blue("请求的 ip 为 -> %+v", ip)
	_ = c.ShouldBindJSON(&device)
	device.Ip = ip
	var count int64
	global.DB.Model(&entity.BanDevice{}).Where("ip=?", ip).Count(&count)
	if count > 0 {
		// 更新
		fmt.Printf("心跳更新 %+v\n", device)
		global.DB.Model(&entity.BanDevice{}).Where("ip=?", ip).Updates(device)
	} else {
		// 添加
		fmt.Printf("没有添加 %+v\n", device)
		global.DB.Create(&device)
	}
	response.OkWithData("发送成功", c)
}
